package com.tanhua.service;

import com.tanhua.domain.LastYear;
import com.tanhua.domain.ThisYear;
import com.tanhua.domain.vo.AnalysisSummaryVo;
import com.tanhua.mapper.AnalysisByDayMapper;
import com.tanhua.mapper.LogMapper;
import com.tanhua.utils.ComputeUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class AnalysisByDayService {

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    @Autowired
    private LogMapper logMapper;

    //统计概要信息
    public ResponseEntity summary() {
        AnalysisSummaryVo summaryVo = new AnalysisSummaryVo();

        Long cumulativeUsers = analysisByDayMapper.queryCumulativeUsers();
        summaryVo.setCumulativeUsers(cumulativeUsers); // 累计用户数

        //String todayStr = new DateTime().toString("yyyy-MM-dd");

        //设置当日登录日期:
        String todayStr = "2020-09-25";

        Long activePassMonth = analysisByDayMapper.queryActivePass(todayStr,new DateTime(todayStr).minusDays(30).toString());
        summaryVo.setActivePassMonth(activePassMonth);//过去30天活跃用户数

        Long activePassWeek = analysisByDayMapper.queryActivePass(todayStr,new DateTime(todayStr).minusDays(7).toString());
        summaryVo.setActivePassWeek(activePassWeek);//过去7天活跃用户

        Integer newUsersToday = logMapper.queryNumRegisteredOrLogin(todayStr, "0102");
        summaryVo.setNewUsersToday(newUsersToday.longValue());//今日新增用户数量

        Integer newUsersYesToday = logMapper.queryNumRegisteredOrLogin(new DateTime(todayStr).minusDays(1).toString(), "0102");
        BigDecimal bigDecimal = ComputeUtil.computeRate(newUsersToday, newUsersYesToday);
        summaryVo.setNewUsersTodayRate(bigDecimal);//今日新增用户涨跌率

        Integer loginTimesToday = logMapper.queryNumRegisteredOrLogin(todayStr, "0101");
        summaryVo.setLoginTimesToday(loginTimesToday.longValue());//今日登录次数

        Integer loginTimesYesToday = logMapper.queryNumRegisteredOrLogin(new DateTime(todayStr).minusDays(1).toString(), "0101");
        BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(loginTimesToday, loginTimesYesToday);
        summaryVo.setLoginTimesTodayRate(loginTimesTodayRate);// 今日登录次数涨跌率

        Integer activeUsersToday = logMapper.queryNumActive(todayStr);
        summaryVo.setActiveUsersToday(activeUsersToday.longValue());//今日活跃用户数量

        Integer activeUsersYesToday = logMapper.queryNumActive(new DateTime(todayStr).minusDays(1).toString());
        BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(activeUsersToday, activeUsersYesToday);
        summaryVo.setActiveUsersTodayRate(activeUsersTodayRate);//今日活跃用户涨跌率

        return ResponseEntity.ok(summaryVo);

    }



    //新增Add(type=101) 活跃用户Active(type=102) 用户留存率Retention rate(type=103)
    public ResponseEntity users(String sd ,String ed ,String type) {
        switch (type) {
            case "101" :
                type = "num_registered";//注册
            case "102" :
                type = "num_active";//活跃
            case "103" :
                type = "num_registered";//留存
        }

        Long queryAARPass = analysisByDayMapper.queryAARPass(sd, ed, type);

        //YearVo yearVo = new Map<>();
        ThisYear thisYear = new ThisYear();
        LastYear lastYear = new LastYear();

        //切割开始日期2020-03-23
        String[] ssd = sd.split("-");
        String stitle = ssd[1];//切割取出月份03

        //切割结束日期2021-09-01
        String[] sed = ed.split("-");
        String etitle = sed[1];//切割取出月份09

        thisYear.setTitle(stitle);
        thisYear.setAmount(queryAARPass.intValue());

        lastYear.setTitle(etitle);
        lastYear.setAmount(queryAARPass.intValue());

        return ResponseEntity.ok("map");
    }
}
